package com.lucy.nepu.provider;

import cn.hutool.core.lang.Validator;
import com.lucy.common.redis.RedisCache;
import com.lucy.nepu.constant.PsConst;
import com.lucy.nepu.loader.PeriodLoader;
import com.lucy.nepu.system.period.Period;
import com.lucy.nepu.system.period.PeriodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author kindear
 * 周期加载器服务提供
 */
@Slf4j
@Service("default")
public class PeriodProvider implements PeriodLoader {
    @Autowired
    PeriodService periodService;

    @Autowired
    RedisCache redisCache;

    @Override
    public String getPeriodId() {
        String periodId = redisCache.getCacheObject(PsConst.REDIS_PERIOD_KEY);
        if (Validator.isEmpty(periodId)){
            // 为空 获取当前默认周期信息
            Period period = periodService.getPeriod();
            periodId = period.getId();
        }
        return periodId;
    }

    @Override
    public Period getPeriod() {
        String periodId = getPeriodId();
        return periodService.getOneUnwrap(periodId);
    }
}
